﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using HCFramework.API.LINQ;

namespace C
{
    public class HtmlModuleController
    {
        public static void Insert(HtmlModule a)
        {
            HCDataContext db = new HCDataContext();
            db.HtmlModules.InsertOnSubmit(a);
            db.SubmitChanges();
        }

        public static void Update(HtmlModule a, HCDataContext db)
        {
            db.SubmitChanges();
        }

        public static void Delete(int id)
        {
            HCDataContext db = new HCDataContext();
            HtmlModule a = db.HtmlModules.Where(
                ik => ik.HtmlModuleID == id).First();
            db.HtmlModules.DeleteOnSubmit(a);
            db.SubmitChanges();
        }

        public static HtmlModule GetByID(int id, HCDataContext db)
        {
            return db.HtmlModules.Where(
                ik => ik.HtmlModuleID == id).FirstOrDefault();
        }

        public static HtmlModule GetByID(int id)
        {
            return GetByID(id, new HCDataContext());
        }

        public static HtmlModule GetCurrentHtmlModuleOrInsert(Trang trang, string userControlId)
        {
            HtmlModule htmlModule = trang.HtmlModules.Where(i => i.UserControlID == userControlId).FirstOrDefault();
            if (htmlModule == null)
            {
                htmlModule = new HtmlModule();
                htmlModule.TrangID = trang.TrangID;
                htmlModule.UserControlID = userControlId;
                htmlModule.Content = string.Empty;
                Insert(htmlModule);
            }
            return htmlModule;
        }

        public static HtmlModule GetCurrentHtmlModuleOrInsert(Trang trang, string userControlId, HCDataContext dc)
        {
            HtmlModule htmlModule = trang.HtmlModules.Where(i => i.UserControlID == userControlId).FirstOrDefault();
            if (htmlModule == null)
            {
                htmlModule = new HtmlModule();
                htmlModule.TrangID = trang.TrangID;
                htmlModule.UserControlID = userControlId;
                htmlModule.Content = string.Empty;
                Insert(htmlModule);
            }
            return htmlModule;
        }
    }
}